import urllib.request
import urllib.parse

#https://46.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112403978023438701115_1710905122405&pn=1&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&wbp2u=|0|0|0|web&fid=f26&fs=m:0+f:8,m:1+f:8&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f26,f22,f11,f62,f128,f136,f115,f152&_=1710905122406

#https://46.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112403978023438701115_1710905122405&pn=2&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&wbp2u=|0|0|0|web&fid=f26&fs=m:0+f:8,m:1+f:8&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f26,f22,f11,f62,f128,f136,f115,f152&_=1710905122424
start_url='https://46.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112403978023438701115_1710905122405&'
end_url='&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&wbp2u=|0|0|0|web&fid=f26&fs=m:0+f:8,m:1+f:8&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f26,f22,f11,f62,f128,f136,f115,f152&_=1710905122424'

headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0'
}
# f6 成交量
# f8 换手
# f9 市营
# f15 最高
# f17 今开
# f20 总市值

def create_request(page):
    data={
        'pn':page
    }
    url=start_url+urllib.parse.urlencode(data)+end_url
    # url='https://quote.eastmoney.com/center/gridlist.html#newshares'
    request=urllib.request.Request(url=url,headers=headers)
    return request

import jsonpath
import json
def get_content(request):
    response=urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    # 利用split切割在获取索引，去除掉不想要的部分
    content = content.split('(')[1].split(')')[0]
    return content
def download(content,page):
    with open('新股交易中心'+str(page)+'.json','w',encoding='utf-8')as fp:
        fp.write(content)
def path(content,page):
    obj = json.load(open('新股交易中心' + str(page) + '.json', 'r', encoding='utf-8'),)
    name_list =jsonpath.jsonpath(obj,'$..f14') #名字
    turnover_list =jsonpath.jsonpath(obj,'$..f6')  #成交量
    change_hands_list =jsonpath.jsonpath(obj,'$..f8')  #换手
    municipal_list =jsonpath.jsonpath(obj,'$..f9')  #市营
    highest_list =jsonpath.jsonpath(obj,'$..f15') #最高
    today_list =jsonpath.jsonpath(obj,'$..f17') #今开
    total_value_list =jsonpath.jsonpath(obj,'$..f20') #总市值
    print('第'+str(page)+'页')
    if(page<=10):
        for i in range(0,20):
            print(name_list[i]+' 成交量:'+str(turnover_list[i])+'万'+' 最高:'+str(highest_list[i])+' 今开:'+str(today_list[i])+' 换手:'+str(change_hands_list[i])+'%'+' 市营:'+str(municipal_list[i])+' 总市值:'+str(total_value_list[i])+'亿')
    else:
        for i in range(1,11):
            print(name_list[i]+' 成交量:'+str(turnover_list[i])+'万'+' 最高:'+str(highest_list[i])+' 今开:'+str(today_list[i])+' 换手:'+str(change_hands_list[i])+'%'+' 市营:'+str(+municipal_list[i])+' 总市值:'+str(total_value_list[i])+'亿')

#入口
if __name__ == '__main__':
    for page in range(1,12):
        # 请求对象的定制
        request = create_request(page)
        # 获取响应数据
        content = get_content(request)
        # # 保存到本地
        download(content,page)
        # 解析
        path(content,page)
